import Vue from 'vue'
import VueRouter from 'vue-router';
Vue.use(VueRouter)

import { authRoutes } from './auth';

import { Notify } from 'vant'

const routes = [
  ...authRoutes,
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/index.vue')
  },
  {
    path: '/news',
    name: 'news',
    component: () => import('../views/news/index.vue')
  }

]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  let hasLogin = false; //通过缓存在本地的登录信息或者vuex来判断权限
  //首页可以进，购物车、我的页面，需要登录
  if (to.name !== 'login' && to.meta.auth) {
    if (!hasLogin) {
      next({ name: 'login' })
    } else {
      next();
    }
  } else if (to.name == 'login' && hasLogin) { 
    //若已登录，则返回之前的页面，给出提示
    // console.log('你已登录,跳转到首页');
    Notify({ type: 'warning', message: '你已登录,退出登录后重试' });
    next({name:from.name});
  } else next()
})
export default router
